﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Strom.Core.Accounts;

namespace Strom.Core.Network.Packets
{
    public class LoginRequestPacket : IPacket
    {
        public static byte ID = 1;

        public void Unpack(BaseClient client, Packet packet)
        {
            byte op = packet.Reader.ReadByte();
            string login = packet.Reader.ReadString();
            string encryptedPassword = packet.Reader.ReadString();

            switch (op)
            {
                case 0:
                    if (!((BaseServer) client.Tag).Database.AccountsManager.AccountExists(login))
                    {
                        ((BaseServer) client.Tag).Database.AccountsList.Add(
                            new Account(new AccountInfos(login, encryptedPassword)));
                        new LoginRequestResponse().Pack(client, op, true);
                    }
                    else
                    {
                        new LoginRequestResponse().Pack(client, op, false);
                    }
                    break;
                case 1:
                    if (((BaseServer) client.Tag).Database.AccountsManager.TryToLogin(login, encryptedPassword))
                    {
                        client.CurrentAccount =
                            ((BaseServer) client.Tag).Database.AccountsManager.GetAccountByLogin(login);
                        client.CurrentAccount.IsOnline = true;
                        new LoginRequestResponse().Pack(client, op, true);
                        new CharactersListPacket().Pack(client, client.CurrentAccount);
                    }
                    else
                    {
                        new LoginRequestResponse().Pack(client, op, false);
                    }
                    break;
                case 2:
                    if (((BaseServer) client.Tag).Database.AccountsManager.TryToLogin(login, encryptedPassword))
                    {
                        client.CurrentAccount =
                            ((BaseServer) client.Tag).Database.AccountsManager.GetAccountByLogin(login);
                        client.CurrentAccount.IsOnline = true;
                        new LoginRequestResponse().Pack(client, op, true);
                        new CharactersListPacket().Pack(client, client.CurrentAccount);
                    }
                    else
                    {
                        new LoginRequestResponse().Pack(client, op, false);
                    }
                    break;
            }
        }

        /// <summary>
        /// Envoyer une demande de connexion ou d'inscription
        /// </summary>
        /// <param name="client">Client qui sera utilisé pour l'envoi du packet</param>
        /// <param name="datas">[0] opération(byte) 0=inscription 1=connexion, 2=connexion editeur [1] login  [2] encrypted password</param>
        public void Pack(BaseClient client, params object[] datas)
        {
            var pw = new PacketWriter();
            pw.Write((byte)(datas[0]));
            pw.Write((string)(datas[1]));
            pw.Write(Utils.Cryptography.ToMD5((string)(datas[2])));
            client.Send(pw.CompilePacket(ID));
        }
    }
}
